package com.ycy.head;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Data
public class HeadLineContentNodeVo extends DformTemplateColumnVo {

    // 默认不传 或者 是TREE , 代表child 是个树形结果
    private String dataStructure;

    private List<HeadLineContentNodeVo> children;

    private List<HeadLineContentNodeVo> leafNodes = new ArrayList<>();

    public static HeadLineContentNodeVo parseConentNode(String jsonString) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        // 忽略JSON字符串中未知的属性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            // 将JSON字符串转换为TreeNode对象列表
            HeadLineContentNodeVo rootAndChildNodes = objectMapper.readValue(jsonString, new TypeReference<HeadLineContentNodeVo>() {
            });
            List<HeadLineContentNodeVo> leafNodes1 = collectLeafNodes(rootAndChildNodes);
            // 检查code属性是否重复
            boolean hasDuplicates = hasDuplicateCodes(leafNodes1);
            if(hasDuplicates){
                throw new Exception("有重复的code属性");
            }
            if (StringUtils.isBlank(rootAndChildNodes.getDataStructure()) || StringUtils.equals("tree", rootAndChildNodes.getDataStructure())) {
                // 树形结构
                rootAndChildNodes.setLeafNodes(leafNodes1);
            } else if (StringUtils.equals("array", rootAndChildNodes.getDataStructure())) {
                //TODO 二维数组结构
            }
            return rootAndChildNodes;
        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }

    // 递归收集所有叶子节点（不包括根节点）
    public static List<HeadLineContentNodeVo> collectLeafNodes(HeadLineContentNodeVo root) {
        List<HeadLineContentNodeVo> leafNodes = new ArrayList<>();
        if (root == null || root.getChildren().isEmpty()) {
            return leafNodes; // 返回空列表，因为不要根
        }
        for (HeadLineContentNodeVo child : root.getChildren()) {
            collectLeafNodesRecursive(child, leafNodes);
        }
        return leafNodes;
    }

    // 辅助递归方法，收集叶子节点
    private static void collectLeafNodesRecursive(HeadLineContentNodeVo node, List<HeadLineContentNodeVo> leafNodes) {
        if (node.getChildren().isEmpty()) {
            // 如果没有子节点，它是一个叶子节点
            leafNodes.add(node);
        } else {
            // 否则，递归地遍历每个子节点
            for (HeadLineContentNodeVo child : node.getChildren()) {
                collectLeafNodesRecursive(child, leafNodes);
            }
        }
    }

    public static boolean hasDuplicateCodes(List<HeadLineContentNodeVo> nodes) {
        return nodes.stream()
                .map(HeadLineContentNodeVo::getCode) // 将每个节点映射到它的code属性
                .collect(Collectors.toSet()) // 收集code值到一个Set中，Set自动去重
                .size() != nodes.size(); // 如果Set的大小不等于原始集合的大小，说明有重复
    }

    // 递归收集每个子节点的路径
    public  List<List<HeadLineContentNodeVo>> createHeadList() {
        HeadLineContentNodeVo root = this;
        List<HeadLineContentNodeVo> currentPath = new ArrayList<>();
        List<List<HeadLineContentNodeVo>> headList = new ArrayList<>();

        if (root == null || root.getChildren().isEmpty()) {
            return headList; // 返回空列表，因为不要根
        }
        for (HeadLineContentNodeVo child : root.getChildren()) {
            createHeadListRecursive(child, currentPath, headList);
        }
        return headList;
    }

    // 辅助递归方法，收集叶子节点
    private static void createHeadListRecursive(HeadLineContentNodeVo node, List<HeadLineContentNodeVo> currentPath, List<List<HeadLineContentNodeVo>> headList) {
        currentPath.add(node);
        if (node.getChildren().isEmpty()) {
            // 如果没有子节点，它是一个叶子节点
            headList.add(new ArrayList<>(currentPath));
        } else {
            // 否则，递归地遍历每个子节点
            for (HeadLineContentNodeVo child : node.getChildren()) {
                createHeadListRecursive(child, currentPath, headList);
            }
        }
        currentPath.remove(currentPath.size() - 1);
    }


}
